//
//  HQAlert.m
//  WenDuEducation
//
//  Created by 黄华强 on 2017/8/1.
//  Copyright © 2017年 黄华强. All rights reserved.
//

#import "HQAlert.h"
#import "HQAlertConst.h"

@implementation HQAlert

//显示指示器
+ (void)showHUDAddedTo:(UIView *)view {
    [MBProgressHUD showHUDAddedTo:view animated:YES];
}

//隐藏指示器
+ (void)hideHUDForView:(UIView *)view {
    [MBProgressHUD hideHUDForView:view animated:YES];
}


+ (void)showMessage:(NSString *)message {
    
    UIWindow *view = [UIApplication sharedApplication].windows.lastObject;
    
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabel.text = message;
    
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:(5 + message.length) / 5.0 > 3 ? 3 : (5 + message.length) / 5.0];
    
}

+ (void)showError:(NSString *)error {

    UIWindow *view = [UIApplication sharedApplication].windows.lastObject;
    
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabel.text = error;
    
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hqa_error"]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:(5 + error.length) / 5.0 > 3 ? 3 : (5 + error.length) / 5.0];
    
}

+ (void)showSuccess:(NSString *)success {

    UIWindow *view = [UIApplication sharedApplication].windows.lastObject;
    
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabel.text = success;
    
    //设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hqa_success"]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 2秒之后再消失
    [hud hideAnimated:YES afterDelay:(5 + success.length) / 5.0 > 3 ? 3 : (5 + success.length) / 5.0];
    
}

+ (void)showLoadGifView:(NSString *)title {
    UIWindow *view = [UIApplication sharedApplication].windows.lastObject;
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.66];
    
    hud.detailsLabel.text = title;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
}


+ (MBProgressHUD *)showProgressHUDAddedTo:(UIView *)view
                                     mode:(MBProgressHUDMode)mode
                                 animated:(BOOL)animated {

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:animated];
    hud.mode = mode;
    hud.contentColor = [UIColor whiteColor];
    hud.animationType = MBProgressHUDAnimationZoom;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f];
    hud.defaultMotionEffectsEnabled = NO;

    return hud;
}


+ (void)hideHUD {
    
    for (UIView *view in [UIApplication sharedApplication].windows) {
        [MBProgressHUD hideHUDForView:view animated:YES];
        
    }
}

+ (void)showAlert:(NSString *)title handler:(void(^)(UIAlertAction *action))handler {
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:title preferredStyle:UIAlertControllerStyleAlert];
    
    [alertC addAction:[UIAlertAction actionWithTitle:HQAlertLocalizedString(HQAlertText_sure) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (handler) {
            handler(action);
        }
    }]];
    
    [[HQAlert getCurrentViewController] presentViewController:alertC animated:YES completion:nil];
    
}

+ (void)showSureAlert:(NSString *)title sureHandler:(void(^)(UIAlertAction *action))sureHandler cancelHandler:(void(^)(UIAlertAction *action))cancelHandler{
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:title preferredStyle:UIAlertControllerStyleAlert];
    
    [alertC addAction:[UIAlertAction actionWithTitle:HQAlertLocalizedString(HQAlertText_sure) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if (sureHandler) {
            sureHandler(action);
        }
        
    }]];
    
    [alertC addAction:[UIAlertAction actionWithTitle:HQAlertLocalizedString(HQAlertText_cancel) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (cancelHandler) {
            cancelHandler(action);
        }
    }]];
    
    
    [[HQAlert getCurrentViewController] presentViewController:alertC animated:YES completion:nil];
    
}


//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentViewController {
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *currentVC = [HQAlert getCurrentVCFrom:rootViewController];
    
    return currentVC;
}

+ (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC {
    
    UIViewController *currentVC ;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        
        currentVC = [HQAlert getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        
        currentVC = [HQAlert getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        
        currentVC = rootVC;
    }
    
    return currentVC;
}



@end
